using System.Collections.Generic;
using DotNetNuke.Common.Utilities;

namespace Code5Systems.FormMaster.Business
{

	#region FormFieldRulesController
	public class FormFieldRulesController
	{
		
		
		
		public List<FormFieldRulesInfo> GetByForm(int formID)
		{
			
			return CBO.FillCollection<FormFieldRulesInfo>(Code5Systems.FormMaster.Data.DataProvider.Instance().GetFormFieldRulesByForm(formID));
			
		}
		
		public List<FormFieldRulesInfo> GetByFormGeneric(int formID)
		{
			List<FormFieldRulesInfo> fieldRules = new List<FormFieldRulesInfo>();
			foreach (FormFieldRulesInfo item in GetByForm(formID))
			{
				fieldRules.Add(item);
			}
			return fieldRules;
		}
		
		public int Add(FormFieldRulesInfo objFormFieldRules)
		{
			
			return System.Convert.ToInt32(Code5Systems.FormMaster.Data.DataProvider.Instance().AddFormFieldRules(objFormFieldRules.FormID, objFormFieldRules.SourceField, objFormFieldRules.SourceFieldName, objFormFieldRules.ContainsValue, objFormFieldRules.FieldType, objFormFieldRules.FormFields));
			
		}
		
		public List<FormFieldRulesInfo> GetSourceFields(int formID)
		{
			return CBO.FillCollection<FormFieldRulesInfo>(Code5Systems.FormMaster.Data.DataProvider.Instance().GetFormFieldRulesByForm(formID));
		}
		
		public List<string> GetTargetFields(int formID)
		{
			List<string> targetFields = new List<string>();
			string[] aryFields = null;
			List<FormFieldRulesInfo> aryRules = default(List<FormFieldRulesInfo>);
			aryRules = CBO.FillCollection<FormFieldRulesInfo>(Code5Systems.FormMaster.Data.DataProvider.Instance().GetFormFieldRulesByForm(formID));
			foreach (FormFieldRulesInfo item in aryRules)
			{
				aryFields = item.FormFields.Split(',');
				for (int i = 0; i <= aryFields.Length - 1; i++)
				{
					targetFields.Add(aryFields[i]);
				}
			}
			
			return targetFields;
			
		}
		
		
		public void Delete(int fieldRuleID)
		{
			
			Code5Systems.FormMaster.Data.DataProvider.Instance().DeleteFormFieldRules(fieldRuleID);
			
		}
		
		public void DeleteFormFieldRulesByForm(int formID)
		{
			Code5Systems.FormMaster.Data.DataProvider.Instance().DeleteFormFieldRulesByForm(formID);
		}
		
	}
#endregion
}
